package com.example.netty.init;

import com.example.netty.handle.SimpleChatServerHandle;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.Delimiters;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;

/**
 * @author PC
 */
public class SimpleChatServerInitializer extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel ch) {
        ChannelPipeline channelPipeline = ch.pipeline();
        //解码器   4096：最大帧长度，单位是字节；   Delimiters.lineDelimiter()：换行分隔符
        channelPipeline.addLast("framer", new DelimiterBasedFrameDecoder(4096, Delimiters.lineDelimiter()));
        //将字节缓冲区解码为字符串
        channelPipeline.addLast("decoder", new StringDecoder());
        //将字符串编码为字节缓冲区
        channelPipeline.addLast("encoder", new StringEncoder());
        channelPipeline.addLast("handler", new SimpleChatServerHandle());
    }
}
